/**
 * Date: 2004-4-9
 * Time: 21:55:48
 */

package org.cnmarc4j.cnmarc;

/**
 * <p><code>Directory</code> is a helper class to build a directory.  </p>
 * <p>The directory is an index to the location of the variable
 * fields within a record. The directory consists of entries. Each
 * directory entry is 12 characters in length. The structure of each
 * entry according to the CNMARC standard is as follows:</p>
 * <pre>
 * TAG     LENGTH_OF_FIELD     STARTING_CHARACTER_POSITION
 * 00-02   03-06               07-11
 * </pre>
 * 
 * <p>The starting character position gives the starting position of
 * the variable field to which the entry corresponds relative to
 * the base address of data of the record. A starting character position
 * or length of field of fewer than five digits is right justified and
 * unused positions contain zero's.</p>
 *
 * <p>The directory of CNMARC is as same as MARC21's.</p>
 *
 */
public class Directory {

    /** number of entries */
    private int entries = 0;

    /**
     * <p>Directory constructor</p>
     *
     * @param dir string contains directory information
     */
    public Directory(String dir) {
        parse(dir);
    }

    /**
     * <p>Parse directory informaion.</p>
     *
     * @param dir the directory string
     */
    private void parse(String dir) {

        // 当前record中著录的字段数目
        entries = dir.length() / 12;

        // String[]数组存储所著录的每个字段的名称
        String[] tag = new String[entries];

        // int[]数组存储所著录的每个字段的实际长度
        int[] length = new int[entries];

        // int[]数组存储所著录的每个字段在整个record中的开始位置
        int[] position = new int[entries];

        // 循环读取每个字段的信息，index是读取整个dir的位置标志
        for (int i = 0, index = 0; i < entries; i++, index += 12) {
            tag[i]
                = dir.substring(index, index + 3);
            length[i]
                = Integer.parseInt(dir.substring(index + 3, index + 7));
            position[i]
                = Integer.parseInt(dir.substring(index + 7, index + 12));
        }

    }

    public int getNumOfEntries() {
        return entries;
    }

}
